package com.lagou.service;

import com.lagou.dao.CodeDao;
import com.lagou.pojo.Code;
import com.laogu.service.EmailService;
import com.laogu.service.ValidateCodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

/**
 * @Author zhangbbk
 * @Description //TODO
 * @Date 2020/5/10 12:47
 **/
@Service
public class CodeServiceImpl implements CodeService{

    @Autowired
    private CodeDao codeDao;

    @Reference(timeout = 50000)
    private EmailService emailService;

    @Override
    public boolean createCode(String email) {
        try{
            int code = (int) ((Math.random() * 9 + 1) * 100000);
            String codeStr = String.valueOf(code);
            // TODO 存入数据库
            Code lagouAuthCode = new Code();
            lagouAuthCode.setCode(codeStr);
            lagouAuthCode.setEmail(email);
            long currentTime = System.currentTimeMillis();
            lagouAuthCode.setCreatetime(String.valueOf(currentTime));
            lagouAuthCode.setExpiretime(String.valueOf(currentTime + 600*1000));
            codeDao.save(lagouAuthCode);
            // 调用邮件微服务发送邮件
            emailService.sendSimpleMail(email,"AuthCode",codeStr);
            System.out.println("===============>>>>>验证码微服务，处理正常.....");
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
